<?php
$loader->requireOnce('includes/OutPatientFlow/TicketItemModel.class.php');
/**
 * Data object model of ticket.
 *
 */
class TicketModel {
	private $ticket_id;
	private $item_list    =   array();

	function __construct($ticketId) {
        $this->ticket_id    =   $ticketId;
        $this->buildTicketModel();
	}

	public function getItemList() {
		return $this->item_list;
	}

	protected function buildTicketModel() {
        $objTicketItem  =   ORDataObject::factory('TicketItem');
        // Step 1: Get ticket items of 'service' type
        $serviceList    =   $objTicketItem->getServiceItemsByTicket($this->ticket_id);

        // Step 2: Get ticket items of 'composite_service' type
        $compositeList  =   $objTicketItem->getCompositeItemsByTicket($this->ticket_id);

        // Step 3: Get ticket items of 'laboratory' type
        $labList    =   $objTicketItem->getLabItemsByTicket($this->ticket_id);

        // Step 4: Get ticket items of 'radiology' type
        $radList    =   $objTicketItem->getRadItemsByTicket($this->ticket_id);

        $itemList   =   array_merge($serviceList, $compositeList, $labList, $radList);
        $this->buildItemModelList($itemList);
	}

	private function buildItemModelList($data) {
		foreach ($data as $row) {
            $objItemModel   =   new TicketItemModel($row['ticket_item_id'], $row['ticket_id'],
                $row['item_id'], $row['service_name'], $row['payment_type'], $row['room_id'],
                $row['room_name'], $row['item_type'], $row['status'], $row['queue_id']);
            $this->item_list[]  =   $objItemModel;
		}
	}

	public function getNextAvailableModelList() {
        $ret['serviceModelList']        =   array();
        $ret['paraclinicalModelList']    =   array();
        foreach ($this->item_list as $itemModel) {
            if ($itemModel->getStatus() == TicketItem::CLOSED || $itemModel->getQueueId() > 0) {
                continue;
            }
            if ($itemModel->getItemType() == TicketItem::SERVICE_TYPE
                    || $itemModel->getItemType() == TicketItem::COMPOSITE_SERVICE) {
                $ret['serviceModelList'][]    =   $itemModel;
            }
            else {
            	$ret['paraclinicalModelList'][]   =   $itemModel;
            }

        }

        return $ret;
	}

	public function getNextAvailableServiceModelList($ticketItemId) {
        $ret    =   array();
        foreach ($this->item_list as $itemModel) {
            if (($itemModel->getStatus() == TicketItem::CLOSED) || ($itemModel->getTicketItemId()== $ticketItemId)) {
                continue;
            }
            if ($itemModel->getItemType() == TicketItem::SERVICE_TYPE
                    || $itemModel->getItemType() == TicketItem::COMPOSITE_SERVICE) {
                $ret[]    =   $itemModel;
            }
        }

        return $ret;
	}

	public function getTicketItemModel($ticketItemId) {
		foreach ($this->item_list as $itemModel) {
            if ($itemModel->getTicketItemId()== $ticketItemId){
                return $itemModel;
            }
		}
	}
}
?>